package com.example.uit_management;

import java.util.ArrayList;
import java.util.HashMap;

import android.app.Activity;
import android.content.Context;
import android.graphics.Paint;
import android.graphics.Typeface;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

public class ArrayAdapterScore extends BaseAdapter {
	// XML node keys
	  TextView Subname_score ; 
      TextView classID_core;        
      TextView scorefinal ;  
      TextView subjectCredit ;
      TextView Semester ;
    private Activity activity;
    private ArrayList<HashMap<String, String>> data;
    private static LayoutInflater inflater=null;   
    
    public ArrayAdapterScore(Activity a, ArrayList<HashMap<String, String>> d) {    	
        activity = a;        
        data=d;
        inflater = (LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }   
    public int getCount() {
        return data.size();
    }

    public Object getItem(int position) {
        return position;
    }

    public long getItemId(int position) {
        return position;
    }
    
    public View getView(int position, View convertView, ViewGroup parent) {
        View vi=convertView;
        if(convertView==null)
            vi = inflater.inflate(R.layout.score_row, null);

        // Get id of row
        Subname_score = (TextView)vi.findViewById(R.id.Subname_score); 
        classID_core = (TextView)vi.findViewById(R.id.classID_core);        
        scorefinal = (TextView)vi.findViewById(R.id.scorefinal);  
        subjectCredit = (TextView) vi.findViewById(R.id.subjectCredit);
        Semester = (TextView) vi.findViewById(R.id.Semester_score);
        
        HashMap<String, String> array = new HashMap<String, String>();
        array = data.get(position);
        String a= "Bao cao :"+ position;
        // Setting all values in listview
        
        //-- Set Ten mon hoc
        Subname_score.setText(array.get(JsonParser_regCourse.KEY_subjectName));
        //Subname_score.setBackgroundResource(R.drawable.border_textview_score);
        //Subname_score.setPaintFlags(Subname_score.getPaintFlags() | Paint.STRIKE_THRU_TEXT_FLAG);
        
        //-- Set lop hoc
        classID_core.setText(array.get(JsonParser_regCourse.KEY_courseID));      
        //classID_core.setBackgroundResource(R.drawable.border_textview_score);
        
        //-- Set diem
        Log.e(ArrayAdapterSchedule.class.toString(), "-----array.get(JsonParser_regCourse.KEY_scoreFinal)"+array.get(JsonParser_regCourse.KEY_scoreFinal));
        scorefinal.setText(array.get(JsonParser_regCourse.KEY_scoreFinal));
        //scorefinal.setBackgroundResource(R.drawable.border_textview_score);
        
        //-- set so tinh chi
        subjectCredit.setText(array.get(JsonParser_regCourse.KEY_subjectCredit));
        //subjectCredit.setBackgroundResource(R.drawable.border_textview_score);
        
        
        //-- set hoc ki
        Semester.setVisibility(View.GONE);
		Semester.setHeight(0);
		Semester.setBackgroundResource(0);
		
		////////////////////////
		// hien hoc ki hoc
		//////////////////////
		// Ap dung cho 1 hoac tat ca hoc ki
        if(Activity_Score.getSemester_Spinner().toString().equals("Tất cả")|| true){ 
        	Log.e(ArrayAdapterScore.class.toString(), "dung 1");
        	if(position !=0){
        		Log.e(ArrayAdapterScore.class.toString(), "dung 2");
        		if(!(data.get(position-1).get(JsonParser_regCourse.KEY_semesterYear).equals(data.get(position).get(JsonParser_regCourse.KEY_semesterYear)))){
        	/*		Subname_score.setBackgroundResource(R.drawable.border_textview_score_red);
        		    classID_core.setBackgroundResource(R.drawable.border_textview_score_red);
        		    scorefinal.setBackgroundResource(R.drawable.border_textview_score_red);*/
        		// thay doi nien khoa
        			Log.e(ArrayAdapterScore.class.toString(), "dung 3.1");
        			// set visible
        			Semester.setVisibility(View.VISIBLE);        			
        			// set text
        			Semester.setText(data.get(position).get(JsonParser_regCourse.KEY_semesterYear)+"_Học Kỳ :"+data.get(position).get(JsonParser_regCourse.KEY_semesterIndex));
        			//set height        
        			//Semester.setHeight(Activity_Score.Height_sore);
        	        //Subname_score.setPaintFlags(Subname_score.getPaintFlags() | Paint.STRIKE_THRU_TEXT_FLAG);
        			// set backgroud
        			Semester.setBackgroundResource(R.drawable.blue_bar7);

        		}else if(!(data.get(position-1).get(JsonParser_regCourse.KEY_semesterIndex).equals(data.get(position).get(JsonParser_regCourse.KEY_semesterIndex)))){
        			/*Subname_score.setBackgroundResource(R.drawable.border_textview_score_red);
        		    classID_core.setBackgroundResource(R.drawable.border_textview_score_red);
        		    scorefinal.setBackgroundResource(R.drawable.border_textview_score_red);*/
        			// thay doi nien khoa
        			Log.e(ArrayAdapterScore.class.toString(), "dung 3.2");
        			// set visible
        			Semester.setVisibility(View.VISIBLE);        			
        			// set text
        			Semester.setText(data.get(position).get(JsonParser_regCourse.KEY_semesterYear)+"_Học Kỳ :"+data.get(position).get(JsonParser_regCourse.KEY_semesterIndex));
        			//set height
        			Semester.setHeight(Activity_Score.Height_sore);
        			// set backgroud
        			Semester.setBackgroundResource(R.drawable.blue_bar7);
        		}        		
        	}        	
        	else{
        		// thay doi nien khoa
    			Log.e(ArrayAdapterSchedule.class.toString(), "dung 3.3: "+Activity_Score.Height_sore);
    			// set visible
    			Semester.setVisibility(View.VISIBLE);        			
    			// set text
    			Semester.setText(data.get(position).get(JsonParser_regCourse.KEY_semesterYear)+"_Học Kỳ :"+data.get(position).get(JsonParser_regCourse.KEY_semesterIndex));
    			// set height        
    			Semester.setHeight(Activity_Score.Height_sore);
    			// set backgroud
    			Semester.setBackgroundResource(R.drawable.blue_bar7);
        	}        	
        	// check diem duoi trung binh hoac ko co diem
            //Check_Score_Fail(array, data, position);
        } 
        return vi;        
    }
	//////////////////////////////
	//danh dau mon hoc da hoc lai
	///////////////////////////////
    /**danh dau mon hoc da hoc lai, hoac rot */
    void Check_Score_Fail(HashMap<String, String> array , ArrayList<HashMap<String,String>> data, int position){

		Log.e(ArrayAdapterScore.class.toString(), "4");
		// vong lap
		for (int i = 0; i < data.size(); i++) {
		Log.e(ArrayAdapterScore.class.toString(), "4.1");
		//mon rot hoac chua co diem
		if(!array.get(JsonParser_regCourse.KEY_scoreFinal).equals("")){			
			if((Float.parseFloat(array.get(JsonParser_regCourse.KEY_scoreFinal).toString()))<=5){
				//Subname_score.setHighlightColor(color.black);
		        Subname_score.setPaintFlags(Subname_score.getPaintFlags() | Paint.STRIKE_THRU_TEXT_FLAG);
		} 
		}else{
		// rot hoac chua co diem
			Log.e(ArrayAdapterScore.class.toString(),"Black");
        	Subname_score.setPaintFlags(Subname_score.getPaintFlags() | Paint.STRIKE_THRU_TEXT_FLAG);
		}
		if(i!=position){
		Log.e(ArrayAdapterScore.class.toString(), "dung 6"+i);
		if(data.get(i).get(JsonParser_regCourse.KEY_subjectID).equals(array.get(JsonParser_regCourse.KEY_subjectID))){
			Subname_score.setTypeface(null,Typeface.BOLD);
			classID_core.setTypeface(null,Typeface.BOLD);
			scorefinal.setTypeface(null,Typeface.BOLD);
			subjectCredit.setTypeface(null,Typeface.BOLD);        			   
			break;
		}else
			Subname_score.setTypeface(null,Typeface.NORMAL);
			classID_core.setTypeface(null,Typeface.NORMAL);
			scorefinal.setTypeface(null,Typeface.NORMAL);
			subjectCredit.setTypeface(null,Typeface.NORMAL); 
		}
		Log.e(ArrayAdapterScore.class.toString(), "dung 7"+i);
		}	
    }
}